﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;
using Teigha.Export_Import;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Teigha.Runtime;

namespace ExportToDWGTool
{
    //class ProductCheckTab
    //{
    //    public string strType; //类别
    //    public string strName;//名称
    //    public string strLng; //经度
    //    public string strLat; //纬度
    //    public string strDescrip; //描述
    //    public string strBZ;//备注
    //}

    //class MarketCheckTab
    //{
    //    public string strBXH; //表箱号
    //    public string strDBBH;//电表局编号
    //    public string strYHBH; //用户编号
    //    public string strYHMC; //用户名称
    //    public string strCZ; //材质
    //    public string strBW; //表位
    //    public string strDescrip; //描述
    //    public string strBZ;//备注
    //}

    //class FlawTab
    //{
    //    public string strType; //类别
    //    public string strName;//名称
    //    public string strFlaw; //存在缺陷     
    //    public string strDescrip; //描述
    //    public string strBZ;//备注
    //}

    class ReadDataFromExcelKG
    {
        public List<string> m_listGT = new List<string>();

        //前端
        public List<GDSBInfo> m_listQD = new List<GDSBInfo>();
        //设备名称及信息
        public Dictionary<string, GDSBInfo> m_DicSBInfo = new Dictionary<string, GDSBInfo>();
        //前端及后端设备
        public Dictionary<string, List<GDSBInfo>> m_DicSB = new Dictionary<string, List<GDSBInfo>>();

        public List<ProductCheckTab> m_listProduct = new List<ProductCheckTab>();
        public Dictionary<string,List<MarketCheckTab>> m_dictMarket = new Dictionary<string, List<MarketCheckTab>>();

        public List<FlawTab> m_listFlaw = new List<FlawTab>();

        public string m_strBYQType;

        public string m_strNM;

        public Database m_pDb = null;

        public double m_xMin = 0;
        public double m_xMax = 0;
        public double m_yMin = 0;
        public double m_yMax = 0;

        public double d = 10;

        private string SetSBNameByType(string strType)
        {
            switch (strType)
            {
                case "pdx":
                    return "GDPDG";
                case "fzx":
                    return "GDDYFZX";
                case "tower":
                    return "杆塔";
                case "qzj":
                    return "墙支架";
                case "jrd":
                    return "GDYHJRD";
                case "kg":
                    return "GDPDKG";
                case "gd":
                    return "拐点";
                case "byq":
                    return "GDPDG";
                case "电缆井":
                    return "圆井";
                case "墙支架":
                    return "GDQZJ";
                case "运行杆塔":
                    return "杆塔";
            }

            return "zd";
        }

        private string SetSBLBByType(string strType)
        {
            switch (strType)
            {
                case "pdx":
                    return "台区";
                case "fzx":
                    return "分支箱";
                case "tower":
                    return "电杆";
                case "qzj":
                    return "墙支架";
                case "jrd":
                    return "表箱";
                case "kg":
                    return "开关";
                case "gd":
                    return "拐点";
                case "byq":
                    return "变压器";
            }

            return "zd";
        }

        public bool ReadSBInfoExcel(string strPath)
        {
            try
            {
                IWorkbook pWb = NPOIExcelHelper.OpenWorkbook(strPath);
                ISheet pSt = NPOIExcelHelper.FirstOrDefaultWorksheetNameStartWith(pWb, "");
                var lines = NPOIExcelHelper.ReadLines(pSt);
                NPOIExcelHelper.CloseWorkbook(pWb);

                for (int i = 0;i<lines.Count;i++)
                {
                    string[] strList = lines[i];
                   
                    GDSBInfo pInfo = new GDSBInfo();
                    pInfo.strMC = strList[1];
                    if(string.IsNullOrEmpty(strList[3]))
                    {
                        pInfo.lng = 0;
                    }
                    else
                    {
                        pInfo.lng = double.Parse(strList[3]);
                    }
                    if(string.IsNullOrEmpty(strList[4]))
                    {
                        pInfo.lat = 0;
                    }
                    else
                    {
                        pInfo.lat = double.Parse(strList[4]);
                    }
                    pInfo.strLineTp = strList[9];
                    pInfo.strLY = SetSBNameByType(strList[10]);
                    pInfo.strLineType = strList[12];

                    if (strList[10]=="pdx")
                    {
                        m_listQD.Add(pInfo);
                        continue;
                    }

                    string strQD = strList[2];

                    if (m_DicSB.Keys.Contains(strQD))
                    {
                        List<GDSBInfo> list = m_DicSB[strQD];
                        list.Add(pInfo);
                        m_DicSB[strQD]= list;
                    }
                    else
                    {
                        List<GDSBInfo> list = new List<GDSBInfo>();
                        list.Add(pInfo);
                        m_DicSB.Add(strQD,list);
                    }

                    if (!m_DicSBInfo.Keys.Contains(pInfo.strMC))
                    {                      
                        m_DicSBInfo.Add(pInfo.strMC, pInfo);                     
                    }

                    //*************************************************
                    ProductCheckTab pProduct = new ProductCheckTab();
                    pProduct.strName = strList[1];
                    pProduct.strLng = strList[3];
                    pProduct.strLat = strList[4];
                    pProduct.strType = SetSBLBByType(strList[10]);
                    pProduct.strDescrip = strList[11];
                    m_listProduct.Add(pProduct);

                    string strFlaw = strList[11];
                    if (!string.IsNullOrEmpty(strFlaw))
                    {
                        FlawTab pFlaw = new FlawTab();
                        pFlaw.strType = SetSBLBByType(strList[10]);
                        pFlaw.strName = strList[1];
                        pFlaw.strFlaw = strFlaw;

                        m_listFlaw.Add(pFlaw);
                    }
                   
                }

            }
            catch (System.Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool ReadYHInfoExcel(string strPath)
        {
            try
            {
                IWorkbook pWb = NPOIExcelHelper.OpenWorkbook(strPath);
                ISheet pSt = NPOIExcelHelper.FirstOrDefaultWorksheetNameStartWith(pWb, "");
                var lines = NPOIExcelHelper.ReadLines(pSt,1, 0, 0);
              
                string strTQ = lines[0][2];
                if (!string.IsNullOrEmpty(strTQ))
                {
                    m_strNM = strTQ;
                }

                lines = NPOIExcelHelper.ReadLines(pSt,0,0,2);
                NPOIExcelHelper.CloseWorkbook(pWb);

                for (int i = 0; i < lines.Count; i++)
                {
                    string[] strList = lines[i];
                    string strQD = strList[10];

                    if (string.IsNullOrEmpty(strQD))
                    {
                        continue;
                    }

                    GDSBInfo pInfo = new GDSBInfo();
                    pInfo.strMC = strList[7];

                    if (string.IsNullOrEmpty(strList[17]))
                    {
                        pInfo.lng = 0;
                    }
                    else
                    {
                        pInfo.lng = double.Parse(strList[17]);
                    }
                    if (string.IsNullOrEmpty(strList[18]))
                    {
                        pInfo.lat = 0;
                    }
                    else
                    {
                        pInfo.lat = double.Parse(strList[18]);
                    }

                    pInfo.strLineTp = strList[9];
                    pInfo.strLY = "GDJLX";

                    ProductCheckTab pProduct = new ProductCheckTab();
                    pProduct.strName = strList[7];
                    pProduct.strLng = strList[17];
                    pProduct.strLat = strList[18];
                    pProduct.strType = "表箱";
                    pProduct.strDescrip = strList[20];                  

                    if (m_DicSB.Keys.Contains(strQD))
                    {
                        List<GDSBInfo> list = m_DicSB[strQD];
                        bool IsHas = false;
                        for (int j = 0; j < list.Count; j++)
                        {
                            GDSBInfo p = list[j];
                            if (p.strMC == pInfo.strMC)
                            {
                                IsHas = true;
                            }
                        }
                        if (IsHas == false)
                        {
                            list.Add(pInfo);
                            m_DicSB[strQD] = list;

                            m_listProduct.Add(pProduct);
                            //*****************************
                            string strFlaw = strList[20];
                            if (!string.IsNullOrEmpty(strFlaw))
                            {
                                FlawTab pFlaw = new FlawTab();
                                pFlaw.strType = "表箱";
                                pFlaw.strName = strList[7];
                                pFlaw.strFlaw = strFlaw;

                                m_listFlaw.Add(pFlaw);
                            }
                        }
                    }
                    else
                    {
                        List<GDSBInfo> list = new List<GDSBInfo>();
                        list.Add(pInfo);
                        m_DicSB.Add(strQD, list);
                     
                        m_listProduct.Add(pProduct);

                        //************************************
                        string strFlaw = strList[20];
                        if (!string.IsNullOrEmpty(strFlaw))
                        {
                            FlawTab pFlaw = new FlawTab();
                            pFlaw.strType = "表箱";
                            pFlaw.strName = strList[7];
                            pFlaw.strFlaw = strFlaw;

                            m_listFlaw.Add(pFlaw);
                        }
                    }

                    MarketCheckTab pMarket = new MarketCheckTab();
                    pMarket.strBXH = strList[3];
                    pMarket.strDBBH = strList[5];
                    pMarket.strYHBH = strList[1];
                    pMarket.strYHMC = strList[2];
                    pMarket.strCZ = strList[15];
                    pMarket.strBW = ""; 
                    pMarket.strDescrip = strList[20];

                    if (!m_dictMarket.Keys.Contains(strList[7]))
                    {
                        List<MarketCheckTab> list = new List<MarketCheckTab>();
                        list.Add(pMarket);
                        m_dictMarket.Add(strList[7],list);
                    }
                    else
                    {
                        List<MarketCheckTab> list = m_dictMarket[strList[7]];
                        list.Add(pMarket);
                        m_dictMarket[strList[7]] =list;
                    }

                }

                foreach(string str in m_dictMarket.Keys)
                {
                    List<MarketCheckTab> list = m_dictMarket[str];
                    int Count = list.Count;

                    for(int i = 0;i<Count;i++)
                    {
                        MarketCheckTab pInfo = list[i];
                        pInfo.strBW = Count.ToString();
                    }
                }

            }
            catch (System.Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool SaveToDWG(string strTempDWG, string strOutSave)
        {
            try
            {
                System.DateTime time = System.DateTime.Now;

                // LogManageMt.m_listLog.Add("时间：" + time.ToString() + "   开始转换，请等待……");

               string strOutDWG = strOutSave + "\\"+m_strNM+".DWG";
                if (File.Exists(strOutSave))
                {
                    File.Delete(strOutSave);
                }

                using (Database pDb = new Database(false, false))// 加参数出错
                {
                    pDb.ReadDwgFile(strTempDWG, FileOpenMode.OpenForReadAndWriteNoShare, false, "");

                    // LogManageMt.m_listLog.Add("开始将转换对象添加到模型空间，请等待……");
                    m_pDb = pDb;

                    BlockTableRecord btr = (BlockTableRecord)pDb.CurrentSpaceId.GetObject(OpenMode.ForWrite);
  
                    for (int i = 0;i< m_listQD.Count;i++)
                    {
                        GDSBInfo pInfo = m_listQD[i];
                        string strQD = pInfo.strMC;
                        if(pInfo.strLY== "GDPDG")
                        {
                            CreatePDX(btr, pInfo);
                        }
                        else
                        {
                            Point2d ptS = Point2d.Origin;
                            CreateSB(btr, pInfo, ref ptS);
                            CreatePLine(btr, strQD, ptS);
                        }
                      
                    }

                    if (m_listQD.Count > 0 )
                    {
                        Teigha.DatabaseServices.Polyline pLine = CreateTK(m_xMin,m_xMax,m_yMin,m_yMax);
                        btr.AppendEntity(pLine);

                        Point3d pt = new Point3d((m_xMin + m_xMax)/2,m_yMax + d+3, 0);
                        DBText pText = CreateText(pt, m_strNM, "HZ");
                        ObjectId OId = btr.AppendEntity(pText);
                        pText = OId.GetObject(OpenMode.ForWrite) as DBText;
                        pText.Height = 3.0;
                        //pText.HorizontalMode = TextHorizontalMode.TextCenter;
                        //pText.VerticalMode = TextVerticalMode.TextVerticalMid;


                        //添加块参照
                        ObjectId BlkId = GetBlkIdByName(pDb, "ZBZ");
                        if (BlkId != ObjectId.Null)
                        {
                            Point3d pt3D = new Point3d(m_xMax +d - 6, m_yMax+d -10, 0);
                            BlockReference blkRef = new BlockReference(pt3D, BlkId);
                            ObjectId Id = btr.AppendEntity(blkRef);

                        }

                        BlkId = GetBlkIdByName(pDb, "TL");
                        if (BlkId != ObjectId.Null)
                        {
                            BlockTableRecord pBlock = BlkId.GetObject(OpenMode.ForWrite) as BlockTableRecord; 

                            Point3d ptText = new Point3d(-7,5.5, 0);
                            DBText pTNM = CreateText(ptText, m_strNM, "HZ");
                            pTNM.Height = 0.6;
                            //pTNM.VerticalMode = TextVerticalMode.TextVerticalMid;
                            //pTNM.HorizontalMode = TextHorizontalMode.TextCenter;
                            pBlock.AppendEntity(pTNM);

                            Point3d pt3D = new Point3d(m_xMax+d-1, m_yMin-d+1, 0);
                            BlockReference blkRef = new BlockReference(pt3D, BlkId);
                            ObjectId Id = btr.AppendEntity(blkRef);

                        }


                        SaveType pSavetype = SaveType.Save12; //Save12 为.dwg Save13 为dxf

                        DwgVersion dwgver = DwgVersion.vAC21; //ACAD2010为 vAC24； ACAD2007 为 vAC21； ACAD2004为vAC18；

                        pDb.SaveAs(@strOutDWG, dwgver);
                    }

                    time = System.DateTime.Now;
                    // LogManageMt.m_listLog.Add("时间：" + time.ToString() + "   转换完成！"); 

                }

                string strOutExcel = strOutSave + "\\配变到户现场核查表(" + m_strNM + ").xls";
                SaveToExcel(@strOutExcel);

                return true;
            }
            catch (System.Exception ex)
            {
                System.DateTime time = System.DateTime.Now;
                // LogManageMt.m_listLog.Add("时间：" + time.ToString() + "   错误信息：" + ex.Message);
                MessageBox.Show(ex.Message);

                return false;
            }
        }

        private Teigha.DatabaseServices.Polyline CreateTK(double xMin, double xMax, double yMin, double yMax)
        {
            Teigha.DatabaseServices.Polyline pLine = null;
            try
            {
                pLine = new Teigha.DatabaseServices.Polyline();
                pLine.AddVertexAt(0, new Point2d(xMin-d, yMin - d), 0, 0, 0);
                pLine.AddVertexAt(1, new Point2d(xMax+d, yMin - d), 0, 0, 0);
                pLine.AddVertexAt(2, new Point2d(xMax + d, yMax + d), 0, 0, 0);
                pLine.AddVertexAt(3, new Point2d(xMin - d, yMax + d), 0, 0, 0);

                pLine.Closed = true;

            }
            catch (System.Exception ex)
            {
                return null;
            }

            return pLine;
        }

        //配电箱及开关设置
        private bool CreatePDX(BlockTableRecord btr, GDSBInfo pInfo)
        {
            try
            {
                double x = 0;
                double y = 0;

                ConvertPlaneCoord.MCT84Bl2xy(pInfo.lng, pInfo.lat, out x, out y);

                Point2d pt0 = new Point2d(y - 1.3, x + 1.3);
                Point2d pt1 = new Point2d(y + 1.3, x + 1.3);
                Point2d pt2 = new Point2d(y + 1.3, x - 1.3);
                Point2d pt3 = new Point2d(y - 1.3, x - 1.3);

                Teigha.DatabaseServices.Polyline pLine = new Teigha.DatabaseServices.Polyline();
                pLine.AddVertexAt(0, pt0, 0, 0, 0);
                pLine.AddVertexAt(1, pt1, 0, 0, 0);
                pLine.AddVertexAt(2, pt2, 0, 0, 0);
                pLine.AddVertexAt(3, pt3, 0, 0, 0);
                pLine.Closed = true;

                ObjectId Id = btr.AppendEntity(pLine);
                GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                Entity pwEnt = null;
                if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                {
                    pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                    pwEnt.Layer = "KGXL1";
                }
            
                if (m_DicSB.Keys.Contains(pInfo.strMC))
                {
                    List<GDSBInfo> list = m_DicSB[pInfo.strMC];

                    int n = list.Count;
                    double d = 2.6/(n+1);

                    pLine = new Teigha.DatabaseServices.Polyline();
                    pLine.AddVertexAt(0, new Point2d(pt0.X+d,pt0.Y-0.5), 0, 0, 0);
                    pLine.AddVertexAt(1, new Point2d(pt1.X-d,pt0.Y-0.5), 0, 0, 0);
                    Id = btr.AppendEntity(pLine);
                    GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                    pwEnt = null;
                    if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = "KGXL1";
                    }

                    //添加配电箱图标

                    pLine = new Teigha.DatabaseServices.Polyline();
                    pLine.AddVertexAt(0, new Point2d(y, x +0.8), 0, 0, 0);
                    pLine.AddVertexAt(1, new Point2d(y, x +2.5), 0, 0, 0);
                    Id = btr.AppendEntity(pLine);
                    GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);
                     pwEnt = null;
                    if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = "KGXL1";
                    }

                    //添加块参照
                    ObjectId BlkId0 = GetBlkIdByName(m_pDb, "GDBYQ");
                    if (BlkId0 == ObjectId.Null)
                    {
                        BlkId0 = GetBlkIdByName(m_pDb, "zd");
                    }

                    Point3d pt3D0 = new Point3d(y, x + 2.5, 0);
                    BlockReference blkRef0 = new BlockReference(pt3D0, BlkId0);
                    Id = btr.AppendEntity(blkRef0);
                    GetExtent(blkRef0, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                    pwEnt = null;
                    if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = "KGXL1";
                    }

                    Point3d pt3dText0 = new Point3d(y, x + 3.8, 0);
                    DBText pText0 = CreateText(pt3dText0,m_strNM, "宋体");

                    pText0.HorizontalMode = TextHorizontalMode.TextMid;
                    pText0.VerticalMode = TextVerticalMode.TextVerticalMid;
                    pText0.Height = 0.15;

                    Id = btr.AppendEntity(pText0);
                    GetExtent(pText0, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                    if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = "KGXL1";
                    }

                    //*******************************************************

                    List<Dictionary<string, Point2d>> listZ = new List<Dictionary<string, Point2d>>();
                    List<Dictionary<string, Point2d>> listY = new List<Dictionary<string, Point2d>>();
                    for (int i = 0; i < list.Count; i++)
                    {
                        GDSBInfo p = list[i];

                        pLine = new Teigha.DatabaseServices.Polyline();
                        pLine.AddVertexAt(0, new Point2d(pt0.X + d*(i+1), pt0.Y - 0.5), 0, 0, 0);
                        pLine.AddVertexAt(1, new Point2d(pt0.X + d * (i + 1), pt0.Y-1.05), 0, 0, 0);
                        Id = btr.AppendEntity(pLine);
                        GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                        pwEnt = null;
                        if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                        {
                            pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                            pwEnt.Layer = "KGXL1";
                        }

                        //添加块参照
                        ObjectId BlkId = GetBlkIdByName(m_pDb, "GDPDKG");
                        if (BlkId == ObjectId.Null)
                        {
                            BlkId = GetBlkIdByName(m_pDb, "zd");
                        }

                        Point3d pt3D = new Point3d(pt0.X + d*(i+1), pt0.Y-1.7334, 0);
                        BlockReference blkRef = new BlockReference(pt3D, BlkId);
                        Id = btr.AppendEntity(blkRef);
                        GetExtent(blkRef, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                        pwEnt = null;
                        if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                        {
                            pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                            pwEnt.Layer = "GDPDKG";
                        }

                        Dictionary<string, Point2d> dicZ = new Dictionary<string, Point2d>();
                        Dictionary<string, Point2d> dicY = new Dictionary<string, Point2d>();

                        if (m_DicSB.Keys.Contains(p.strMC))
                        {
                            Point3d pt3dText = new Point3d(pt0.X + d * (i + 1)+0.1, pt0.Y - 1.7334, 0);
                            DBText pText = CreateText(pt3dText, p.strMC, "宋体");

                            pText.HorizontalMode = TextHorizontalMode.TextMid;
                            pText.VerticalMode = TextVerticalMode.TextVerticalMid;
                            pText.Height = 0.08;
                            pText.Rotation = 270 * Math.PI / 180;

                            Id = btr.AppendEntity(pText);
                            GetExtent(pText, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                            if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                            {
                                pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                                pwEnt.Layer = pInfo.strLY;                               
                            }

                            pLine = new Teigha.DatabaseServices.Polyline();
                            pLine.AddVertexAt(0, new Point2d(pt0.X + d * (i + 1), pt0.Y - 2.27), 0, 0, 0);
                            pLine.AddVertexAt(1, new Point2d(pt0.X + d * (i + 1), pt0.Y - 3.5), 0, 0, 0);
                            Id = btr.AppendEntity(pLine);
                            GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                            pwEnt = null;
                            if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                            {
                                pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                                pwEnt.Layer = "KGXL1";
                            }
                            GDSBInfo pp = m_DicSB[p.strMC][0];
                            double px = 0;
                            double py = 0;

                            ConvertPlaneCoord.MCT84Bl2xy(pp.lng, pp.lat, out px, out py);

                            if(pt0.X + d * (i + 1)>= py)
                            {
                                if (!dicZ.Keys.Contains(p.strMC))
                                {
                                    dicZ.Add(p.strMC, new Point2d(pt0.X + d * (i + 1), pt0.Y - 3.5));
                                }
                            }
                            else
                            {
                                if (!dicY.Keys.Contains(p.strMC))
                                {
                                    dicY.Add(p.strMC, new Point2d(pt0.X + d * (i + 1), pt0.Y - 3.5));
                                }
                            }
                       
                            //int m = 0;
                            //if(n%2==1)
                            //{
                            //    m = (n + 1) / 2;
                            //}
                            //else
                            //{
                            //    m = n/ 2;
                            //}

                            //if (i >= m)
                            //{
                            //    CreateKGSB(btr, dic, xOffY, "右", yOffY);

                            //    yOffY = yOffY - 0.3;
                            //    xOffY = xOffY + d;
                            //}
                            //else
                            //{
                            //    CreateKGSB(btr, dic, xOffZ, "左", yOffZ);
                            //    yOffZ = yOffZ - 0.3;
                            //    xOffZ = xOffZ - d;
                            //}
                        }
                        else
                        {
                            Point3d pt3dText = new Point3d(pt0.X + d * (i + 1) + 0.1, pt0.Y - 1.7334, 0);
                            DBText pText = CreateText(pt3dText, p.strMC+"(备用)", "宋体");

                            pText.HorizontalMode = TextHorizontalMode.TextMid;
                            pText.VerticalMode = TextVerticalMode.TextVerticalMid;
                            pText.Rotation = 270 * Math.PI / 180;
                            pText.Height = 0.08;
                            Id = btr.AppendEntity(pText);
                            GetExtent(pText, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                            if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                            {
                                pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                                pwEnt.Layer = pInfo.strLY;
                            }
                        }

                        if(dicZ.Count>0)
                        {                         
                            listZ.Add(dicZ);
                        }

                        if (dicY.Count > 0)
                        {
                            listY.Add(dicY);
                        }

                    }

                    double yOffZ = 0;
                    double yOffY = 0;
                    double xOffZ = -2;
                    double xOffY = 2;

                    for (int i = 0;i<listZ.Count;i++)
                    {
                        Dictionary<string, Point2d> dic = listZ[i];
                        CreateKGSB(btr, dic, xOffZ, "左", yOffZ);
                        yOffZ = yOffZ - 0.3;
                        xOffZ = xOffZ - d;
                    }

                    for (int i = listY.Count-1; i >=0; i--)
                    {
                        Dictionary<string, Point2d> dic = listY[i];
                        CreateKGSB(btr, dic, xOffY, "右", yOffY);

                        yOffY = yOffY - 0.3;
                        xOffY = xOffY + d;
                    }
                }




            }
            catch(System.Exception ex)
            {
                return false;
            }
            return true;
       }

        private bool CreateKGSB(BlockTableRecord btr, Dictionary<string, Point2d> dic,double jj,string strFX, double yOff)
        {
            try
            {
                if (strFX == "左")
                {
                    double xOff = -2;
                    foreach (string str in dic.Keys)
                    {
                        Point2d pt = dic[str];

                        Point2d pt0 = new Point2d(pt.X, pt.Y + yOff);
                        Point2d pt1 = new Point2d(pt.X + xOff, pt.Y + yOff);
                        Teigha.DatabaseServices.Polyline pLine = new Teigha.DatabaseServices.Polyline();
                        pLine.AddVertexAt(0, pt, 0, 0, 0);
                        pLine.AddVertexAt(1, pt0, 0, 0, 0);
                        pLine.AddVertexAt(2, pt1, 0, 0, 0);

                        ObjectId Id = btr.AppendEntity(pLine);
                        GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                        Entity pwEnt = null;
                        if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                        {
                            pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                            pwEnt.Layer = "KGXL1";
                        }

                        CreatePLine(btr, str, pt1);

                        xOff = xOff + jj;
                    }
                }
                else
                {
                    double xOff = 2;

                    foreach (string str in dic.Keys)
                    {
                        Point2d pt = dic[str];

                        Point2d pt0 = new Point2d(pt.X, pt.Y + yOff);
                        Point2d pt1 = new Point2d(pt.X + xOff, pt.Y + yOff);
                        Teigha.DatabaseServices.Polyline pLine = new Teigha.DatabaseServices.Polyline();
                        pLine.AddVertexAt(0, pt, 0, 0, 0);
                        pLine.AddVertexAt(1, pt0, 0, 0, 0);
                        pLine.AddVertexAt(2, pt1, 0, 0, 0);

                        ObjectId Id = btr.AppendEntity(pLine);
                        GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                        Entity pwEnt = null;
                        if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                        {
                            pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                            pwEnt.Layer = "KGXL1";
                        }

                        CreatePLine(btr, str, pt1);

                    }

                }

            }
            catch(System.Exception ex)
            {
                return false;
            }

            return true;
        }

        private bool CreatePLine(BlockTableRecord btr,string strQD, Point2d ptS)
        {
            try
            {
                if (m_DicSB.Keys.Contains(strQD))
                {
                    List<GDSBInfo> list = m_DicSB[strQD];

                    for(int i = 0;i<list.Count;i++)
                    {
                        GDSBInfo pInfo = list[i];
                        Point2d ptE = Point2d.Origin;
                        CreateSB(btr, pInfo, ref ptE);

                        //计量箱前添加用户接入点
                        if (pInfo.strLY == "GDJLX")
                        {
                            GDSBInfo pInfoQD = null;
                            if (m_DicSBInfo.Keys.Contains(strQD))
                            {
                                pInfoQD = m_DicSBInfo[strQD];
                            }

                            if (pInfoQD != null && pInfoQD.strLY == "GDYHJRD")
                            {
                               
                                Teigha.DatabaseServices.Polyline pLine0 = new Teigha.DatabaseServices.Polyline();
                                pLine0.AddVertexAt(0, ptS, 0, 0, 0);
                                pLine0.AddVertexAt(1, ptE, 0, 0, 0);

                                ObjectId Id0 = btr.AppendEntity(pLine0);
                                GetExtent(pLine0, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                                Entity pwEnt0 = null;
                                if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                                {
                                    pwEnt0 = (Entity)Id0.Open(OpenMode.ForWrite);
                                    pwEnt0.Layer = "KGXL1";
                                }

                                continue;
                            }

                            Point2d ptMid = new Point2d(ptE.X, (ptE.Y + ptS.Y) / 2);
                            Teigha.DatabaseServices.Polyline pLine = new Teigha.DatabaseServices.Polyline();
                            pLine.AddVertexAt(0, ptS, 0, 0, 0);
                            pLine.AddVertexAt(1, ptMid, 0, 0, 0);
                            pLine.AddVertexAt(2, ptE, 0, 0, 0);

                            ObjectId Id = btr.AppendEntity(pLine);
                            GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                            Entity pwEnt = null;
                            if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                            {
                                pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                                pwEnt.Layer = "KGXL1";
                            }

                            //添加块参照
                            ObjectId BlkId = GetBlkIdByName(m_pDb, "GDYHJRD");
                            if (BlkId == ObjectId.Null)
                            {
                                BlkId = GetBlkIdByName(m_pDb, "zd");
                            }

                            Point3d pt3D = new Point3d(ptE.X, (ptE.Y + ptS.Y) / 2, 0);
                            BlockReference blkRef = new BlockReference(pt3D, BlkId);
                            Id = btr.AppendEntity(blkRef);
                            GetExtent(blkRef, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                            pwEnt = null;
                            if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                            {
                                pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                                pwEnt.Layer = "GDYHJRD";
                            }

                            CreatePLine(btr, pInfo.strMC, ptE);

                        }
                        else
                        {
                            Teigha.DatabaseServices.Polyline pLine = new Teigha.DatabaseServices.Polyline();
                            pLine.AddVertexAt(0, ptS, 0, 0, 0);
                            pLine.AddVertexAt(1, ptE, 0, 0, 0);

                            ObjectId Id = btr.AppendEntity(pLine);
                            GetExtent(pLine, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                            Entity pwEnt = null;
                            if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                            {
                                pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                                pwEnt.Layer = "KGXL1";
                                if (pInfo.strLineType == "入地")
                                {
                                    pwEnt.Color = Color.FromColor(System.Drawing.Color.Blue);
                                }
                            }
                            //添加线长
                            
                            string strText = pLine.Length.ToString("F2")+"米";
                            if (!string.IsNullOrEmpty(pInfo.strLineTp))
                            {
                                strText = pInfo.strLineTp + "/" + strText;
                            }

                            if(!string.IsNullOrEmpty(pInfo.strLineType))
                            {
                                strText = strText + "(" + pInfo.strLineType + ")";
                            }
                            Point3d pt3dText = new Point3d((ptS.X+ptE.X)/2, (ptS.Y + ptE.Y) / 2+0.1, 0);
                            DBText pText = CreateText(pt3dText, strText, "宋体");

                            pText.HorizontalMode = TextHorizontalMode.TextMid;
                            pText.VerticalMode = TextVerticalMode.TextBottom;
                            double rad = CalculateLineAngle(ptS, ptE);
                            pText.Height = 0.1;

                            pText.Rotation = rad;

                            Id = btr.AppendEntity(pText);
                            GetExtent(pText, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                            if (FindCADLayerByNM(m_pDb, "KGXL1") == true)
                            {
                                pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                                pwEnt.Layer = "KGXL1";
                            }

                           CreatePLine(btr, pInfo.strMC, ptE);

                        }
                    }                   
                }

            }
            catch (System.Exception ex)
            {
                return false;
            }

            return true;
        }

        private bool CreateSB(BlockTableRecord btr,GDSBInfo pInfo,ref Point2d pt)
        {
            try
            {
                double x = 0;
                double y = 0;

                ObjectId BlkId = GetBlkIdByName(m_pDb, pInfo.strLY);
                if (BlkId == ObjectId.Null)
                {
                    BlkId = GetBlkIdByName(m_pDb, "zd");
                }

                ConvertPlaneCoord.MCT84Bl2xy(pInfo.lng, pInfo.lat, out x, out y);
                Point3d pt3D = new Point3d(y, x, 0);

                pt = new Point2d(y, x);

                BlockReference blkRef = new BlockReference(pt3D, BlkId);
                ObjectId Id = btr.AppendEntity(blkRef);
                GetExtent(blkRef, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                Entity pwEnt = null;
                if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                {
                    pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                    pwEnt.Layer = pInfo.strLY;
                }

                if (pInfo.strLY == "GDGT")
                {
                    BlkId = GetBlkIdByName(m_pDb, "zd");
                    blkRef = new BlockReference(pt3D, BlkId);
                    Id = btr.AppendEntity(blkRef);
                    GetExtent(blkRef, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                    if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = pInfo.strLY;

                        HyperLinkCollection linkCollection = pwEnt.Hyperlinks;

                        //Create a new hyperlink

                        HyperLink hyperLink = new HyperLink();

                        hyperLink.Description = "F:\\10kV闸西122线三丰村程庄组2号变支线01号铭牌.jpg"; 

                        hyperLink.Name = "ADN DevBlog";

                        hyperLink.SubLocation = "F:\\10kV闸西122线三丰村程庄组2号变支线01号铭牌.jpg";

                        //Add the hyperlink to the collection

                        linkCollection.Add(hyperLink);

                    }

                    m_listGT.Add(pInfo.strMC);
                }

                if (pInfo.strLY == "GDQZJ")
                {
                    Point3d pt3dText = new Point3d(y - 1.5, x - 0.3, 0);
                    DBText pText = CreateText(pt3dText, pInfo.strMC, "宋体");

                    pText.HorizontalMode = TextHorizontalMode.TextRight;
                    pText.VerticalMode = TextVerticalMode.TextTop;

                    Id = btr.AppendEntity(pText);
                    GetExtent(pText, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                    if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = pInfo.strLY;
                    }
                }
                else if (pInfo.strLY == "GDJLX")
                {
                    Point3d pt3dText = new Point3d(y + 0.35, x + 0.006, 0);
                    DBText pText = CreateText(pt3dText, pInfo.strMC, "宋体");

                    pText.HorizontalMode = TextHorizontalMode.TextLeft;
                    pText.VerticalMode = TextVerticalMode.TextVerticalMid;

                    Id = btr.AppendEntity(pText);
                    GetExtent(pText, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                    if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = pInfo.strLY;
                    }
                }
                else
                {
                    Point3d pt3dText = new Point3d(y, x + 0.5, 0);
                    DBText pText = CreateText(pt3dText, pInfo.strMC, "宋体");

                    pText.HorizontalMode = TextHorizontalMode.TextMid;
                    pText.VerticalMode = TextVerticalMode.TextVerticalMid;

                    Id = btr.AppendEntity(pText);
                    GetExtent(pText, ref m_xMin, ref m_xMax, ref m_yMin, ref m_yMax);

                    if (FindCADLayerByNM(m_pDb, pInfo.strLY) == true)
                    {
                        pwEnt = (Entity)Id.Open(OpenMode.ForWrite);
                        pwEnt.Layer = pInfo.strLY;
                    }

                }
            }
            catch(System.Exception ex)
            {
                return false;
            }
            return true;
        }

        private double CalculateLineAngle(Point2d pt1, Point2d pt2)
        {
            double J = 0;
            double x = pt2.X - pt1.X;
            double y = pt2.Y - pt1.Y;

            if (x == 0 && y < 0)
            {
                J = 0;
            }
            else
            {
                double k = y / x;
                J = Math.Atan2(y, x); //弧度
            }

            double ang = J * 180.0 / Math.PI; //角度

            if (ang < 0)
            {
                ang = ang + 360;
            }

            if (ang == 0)
            {
                ang = 270;
            }

            if (ang > 90 && ang <= 180)
            {
                ang = ang + 180;
            }
            else if (ang > 180 && ang < 270)
            {
                ang = ang - 180;
            }

            double rad = ang * Math.PI / 180;

            return rad;
        }

        private ObjectId GetTextStyleIdByName(Database pDb, string strName)
        {
            ObjectId Oid = ObjectId.Null;
            try
            {
                using (TextStyleTable styleTable = (TextStyleTable)pDb.TextStyleTableId.GetObject(OpenMode.ForRead))
                {
                    foreach (ObjectId id in styleTable)
                    {
                        using (TextStyleTableRecord pTextStyle = (TextStyleTableRecord)id.Open(OpenMode.ForRead))
                        {
                            if (pTextStyle.Name == strName)
                            {
                                Oid = id;
                                break;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
            return Oid;
        }

        private DBText CreateText(Point3d pt, string strText, string strTextStyleName)
        {
            DBText pText = new DBText();
            pText.Position = pt;
            pText.TextString = strText;

            ObjectId styleId = GetTextStyleIdByName(m_pDb, strTextStyleName);
            pText.TextStyleId = styleId;
            pText.Height = 0.12;
            pText.Rotation = 0;
            pText.WidthFactor = 1;
            pText.Oblique = 0;
            pText.VerticalMode = TextVerticalMode.TextVerticalMid;
            pText.HorizontalMode = TextHorizontalMode.TextCenter;

            pText.AlignmentPoint = pt;
            pText.AdjustAlignment(m_pDb);//设置文字对齐


            return pText;
        }

        private ObjectId GetBlkIdByName(Database pDb, string strName)
        {
            ObjectId Oid = ObjectId.Null;
            try
            {
                TransactionManager pTMgr = pDb.TransactionManager;
                using (Transaction pTransac = pTMgr.StartTransaction())
                {
                    BlockTable pBTable = pTMgr.GetObject(pDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                    foreach (ObjectId id in pBTable)
                    {

                        BlockTableRecord pBlock = pTMgr.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
                        string str = pBlock.Name;
                        if (pBlock.Name == strName)
                        {
                            Oid = id;
                            break;
                        }

                        // m_Dic.Add(str, id);
                    }

                    pTransac.Commit();
                }



            }
            catch (System.Exception ex)
            {

            }
            //  m_Dic.Add("9999", Oid);
            return Oid;
        }

        public bool FindCADLayerByNM(Database pDB, string strName)
        {

            if (pDB == null) return false;

            try
            {
                TransactionManager pTMgr = pDB.TransactionManager;
                using (Transaction pTransac = pTMgr.StartTransaction())
                {
                    LayerTable pLayerTable = pTMgr.GetObject(pDB.LayerTableId, OpenMode.ForRead) as LayerTable;

                    SymbolTableEnumerator pIterator = pLayerTable.GetEnumerator();

                    while (pIterator.MoveNext())
                    {
                        ObjectId objId = pIterator.Current;
                        LayerTableRecord pLayer = pTMgr.GetObject(objId, OpenMode.ForRead) as LayerTableRecord;

                        string strLy = pLayer.Name;

                        if (strLy == strName)
                        {
                            pTransac.Commit();
                            return true;
                        }

                    }

                    pTransac.Commit();
                }
                return false;
            }
            catch (System.Exception ex)
            {
                return false;
            }

        }

        private bool GetExtent(Entity pEnt,ref double xMin, ref double xMax, ref double yMin, ref double yMax)
        {
            try
            {
                if(pEnt==null)
                {
                    return false;
                }

                Extents3d exten = pEnt.GeometricExtents;
                Point3d pt1 = exten.MinPoint;
                Point3d pt2 = exten.MaxPoint;

                if(xMin==0)
                {
                    xMin = pt1.X;
                }

                if (yMin == 0)
                {
                    yMin = pt1.Y;
                }

                if (pt1.X< xMin)
                {
                    xMin = pt1.X;
                }
                if(pt1.Y < yMin)
                {
                    yMin = pt1.Y;
                }

                if (pt2.X >xMax)
                {
                    xMax = pt2.X;
                }
                if (pt2.Y > yMax)
                {
                    yMax = pt2.Y;
                }

            }
            catch(System.Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool SaveToExcel(string strPath)
        {
            try
            {
                IWorkbook  workbook = NPOIExcelHelper.CreateWorkbook();
                ISheet sheet = NPOIExcelHelper.CreateSheetByName(workbook,"生产");
                CreateTableProduct( workbook, sheet, "配变到户现场—生产核查表");

                sheet = NPOIExcelHelper.CreateSheetByName(workbook, "营销");
                CreateTableMarket(workbook, sheet, "配变到户现场—营销核查表");

                sheet = NPOIExcelHelper.CreateSheetByName(workbook, "缺陷表");
                CreateTableFlaw(workbook, sheet, "配变到户现场—缺陷核查表");

                NPOIExcelHelper.SaveExcel(workbook, strPath);
            }
            catch(System.Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool CreateTableProduct(IWorkbook workbook, ISheet worksheet, string strTabName)
        {
            try
            {
                if (worksheet != null)
                {
                    ICell sheetTitle = worksheet.CreateRow(0).CreateCell(0);                   
                    sheetTitle.SetCellValue(strTabName);
                    sheetTitle.CellStyle = NPOIExcelHelper.GetTitleCellStyle(workbook, 15, NPOI.HSSF.Util.HSSFColor.Black.Index);
                    worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 8));  //合并单元格

                    string str = "台区：" + m_strNM;
                    ICell sheetTitle1 = worksheet.CreateRow(1).CreateCell(0);                   

                    sheetTitle1.SetCellValue(str);
                    sheetTitle1.CellStyle = NPOIExcelHelper.GetTitleCellStyle(workbook, 13, NPOI.HSSF.Util.HSSFColor.Black.Index);
                    worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 8));  //合并单元格

                    IRow sheetHead = worksheet.CreateRow(2);

                    //表头数据
                    ICellStyle cellStyle1 = NPOIExcelHelper.GetCellStyle(workbook, 2);
                    ICellStyle cellStyle2 = NPOIExcelHelper.GetCellStyle(workbook, 0);

                    ICell cell = sheetHead.CreateCell(0);
                    cell.SetCellValue("序号");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(1);
                    cell.SetCellValue("类别");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(2);
                    cell.SetCellValue("名称");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(3);
                    cell.SetCellValue("经度X");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(4);
                    cell.SetCellValue("纬度Y");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(5);
                    cell.SetCellValue("现场有无铭牌");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(6);
                    cell.SetCellValue("铭牌是否正确");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(7);
                    cell.SetCellValue("描述");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(8);
                    cell.SetCellValue("备注");
                    cell.CellStyle = cellStyle1;

                    for (int i = 0;i< m_listProduct.Count;i++)
                    {
                        ProductCheckTab pInfo = m_listProduct[i];
                        IRow row = worksheet.CreateRow(3+i);

                        cell = row.CreateCell(0);
                        cell.SetCellValue(i+1);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(1);
                        cell.SetCellValue(pInfo.strType);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(2);
                        cell.SetCellValue(pInfo.strName);
                        cell.CellStyle = cellStyle2;
                    
                        cell = row.CreateCell(3);
                        cell.SetCellValue(pInfo.strLng);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(4);
                        cell.SetCellValue(pInfo.strLat);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(5);
                        cell.SetCellValue("");
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(6);
                        cell.SetCellValue("");
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(7);
                        cell.SetCellValue(pInfo.strDescrip);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(8);
                        cell.SetCellValue(pInfo.strBZ);
                        cell.CellStyle = cellStyle2;

                    }

                }
            }
            catch (System.Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool CreateTableMarket(IWorkbook workbook, ISheet worksheet, string strTabName)
        {
            try
            {
                if (worksheet != null)
                {
                    ICell sheetTitle = worksheet.CreateRow(0).CreateCell(0);
                    sheetTitle.SetCellValue(strTabName);
                    sheetTitle.CellStyle = NPOIExcelHelper.GetTitleCellStyle(workbook, 15, NPOI.HSSF.Util.HSSFColor.Black.Index);
                    worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 8));  //合并单元格

                    string str = "台区：" + m_strNM;
                    ICell sheetTitle1 = worksheet.CreateRow(1).CreateCell(0);
                    sheetTitle1.SetCellValue(str);
                    sheetTitle1.CellStyle = NPOIExcelHelper.GetTitleCellStyle(workbook, 13, NPOI.HSSF.Util.HSSFColor.Black.Index);
                    worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 8));  //合并单元格

                    IRow sheetHead = worksheet.CreateRow(2);

                    //表头数据
                    ICellStyle cellStyle1 = NPOIExcelHelper.GetCellStyle(workbook, 2);
                    ICellStyle cellStyle2 = NPOIExcelHelper.GetCellStyle(workbook, 0);

                    ICell cell = sheetHead.CreateCell(0);
                    cell.SetCellValue("序号");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(1);
                    cell.SetCellValue("表箱号");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(2);
                    cell.SetCellValue("电表局编号");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(3);
                    cell.SetCellValue("用户编号");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(4);
                    cell.SetCellValue("用户名称");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(5);
                    cell.SetCellValue("材质");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(6);
                    cell.SetCellValue("表位");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(7);
                    cell.SetCellValue("描述");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(8);
                    cell.SetCellValue("备注");
                    cell.CellStyle = cellStyle1;

                    int n = 0;
                    foreach (string key in m_dictMarket.Keys)
                    {
                        List<MarketCheckTab> list = m_dictMarket[key];
                        int Count = list.Count;

                        for (int i = 0; i < Count; i++)
                        {
                            MarketCheckTab pInfo = list[i];

                            n++;

                            IRow row = worksheet.CreateRow(n+2);

                            cell = row.CreateCell(0);
                            cell.SetCellValue(n);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(1);
                            cell.SetCellValue(pInfo.strBXH);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(2);
                            cell.SetCellValue(pInfo.strDBBH);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(3);
                            cell.SetCellValue(pInfo.strYHBH);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(4);
                            cell.SetCellValue(pInfo.strYHMC);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(5);
                            cell.SetCellValue(pInfo.strCZ);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(6);
                            cell.SetCellValue(pInfo.strBW);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(7);
                            cell.SetCellValue(pInfo.strDescrip);
                            cell.CellStyle = cellStyle2;

                            cell = row.CreateCell(8);
                            cell.SetCellValue(pInfo.strBZ);
                            cell.CellStyle = cellStyle2;

                        }
                    }

                }
            }
            catch (System.Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool CreateTableFlaw(IWorkbook workbook, ISheet worksheet, string strTabName)
        {
            try
            {
                if (worksheet != null)
                {
                    ICell sheetTitle = worksheet.CreateRow(0).CreateCell(0);
                    sheetTitle.SetCellValue(strTabName);
                    sheetTitle.CellStyle = NPOIExcelHelper.GetTitleCellStyle(workbook, 15, NPOI.HSSF.Util.HSSFColor.Black.Index);
                    worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 5));  //合并单元格

                    string str = "台区：" + m_strNM;
                    ICell sheetTitle1 = worksheet.CreateRow(1).CreateCell(0);
                    sheetTitle1.SetCellValue(str);
                    sheetTitle1.CellStyle = NPOIExcelHelper.GetTitleCellStyle(workbook, 13, NPOI.HSSF.Util.HSSFColor.Black.Index);
                    worksheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 5));  //合并单元格

                    IRow sheetHead = worksheet.CreateRow(2);

                    //表头数据
                    ICellStyle cellStyle1 = NPOIExcelHelper.GetCellStyle(workbook, 2);
                    ICellStyle cellStyle2 = NPOIExcelHelper.GetCellStyle(workbook, 0);

                    ICell cell = sheetHead.CreateCell(0);
                    cell.SetCellValue("序号");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(1);
                    cell.SetCellValue("类别");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(2);
                    cell.SetCellValue("名称");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(3);
                    cell.SetCellValue("存在缺陷");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(4);
                    cell.SetCellValue("描述");
                    cell.CellStyle = cellStyle1;

                    cell = sheetHead.CreateCell(5);
                    cell.SetCellValue("备注");
                    cell.CellStyle = cellStyle1;

                    for (int i = 0; i < m_listFlaw.Count; i++)
                    {
                        FlawTab pInfo = m_listFlaw[i];
                        IRow row = worksheet.CreateRow(3 + i);

                        cell = row.CreateCell(0);
                        cell.SetCellValue(i + 1);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(1);
                        cell.SetCellValue(pInfo.strType);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(2);
                        cell.SetCellValue(pInfo.strName);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(3);
                        cell.SetCellValue(pInfo.strFlaw);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(4);
                        cell.SetCellValue(pInfo.strDescrip);
                        cell.CellStyle = cellStyle2;

                        cell = row.CreateCell(5);
                        cell.SetCellValue(pInfo.strBZ);
                        cell.CellStyle = cellStyle2;

                    }

                }
            }
            catch (System.Exception ex)
            {
                return false;
            }

            return true;
        }

    }
}
